package org.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DirtyWorldFilter {
    public static class Node extends HashMap<Character,Node>{
        public Node(boolean isEnd) {
            super();
            this.isEnd = isEnd;
        }

        public Node(){
            super();
            this.isEnd=false;
        }

        boolean isEnd;

        public boolean isEnd() {
            return isEnd;
        }

        public void setEnd(boolean end) {
            isEnd = end;
        }

    }
    public static Node dicts=new Node();
    //为叶子节点
    public static final Node LAST_NODE=new Node(true);
    static {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("草你妈");
        strings.add("草你妈逼");
        strings.add("傻逼");
        strings.add("傻子");
        strings.add("傻瓜");
        strings.add("肖锋");
        putDicts(strings);
    }

    public static String doFilter(String text){
        Node map=dicts;
        int beginIndex=-1;//记录开始下标
        int end=-1;
        char[] chars = text.toCharArray();
        for (int i = 0; i < text.length(); i++) {
            char c = chars[i];
            Node nextNode = map.get(c);
            if (nextNode!=null){
                if (beginIndex==-1){
                    beginIndex=i;
                }
                if (nextNode.isEnd){
                    end=i;
                }
                if (nextNode==LAST_NODE){
                    for (int j = beginIndex; j <=end; j++) {
                        chars[j]='*';
                    }
                    beginIndex=-1;
                    end=-1;
                    map=dicts;
                    continue;
                }
                map=nextNode;
            }else {
                if (beginIndex!=-1){
                    if (end!=-1){
                        for (int j = beginIndex; j <=end; j++) {
                            chars[j]='*';
                        }
                        end=-1;
                    }
                    beginIndex=-1;
                }
                if (map!=dicts){
                    map=dicts;
                    i--;//使用原字典查看
                }
            }
        }
        if (beginIndex!=-1){
            if (end!=-1){
                for (int j = beginIndex; j <=end; j++) {
                    chars[j]='*';
                }
            }
        }
        return new String(chars);
    }

    public static void putDicts(List<String> words){
        for (String str:words){
            putStringIntoDicts(str);
        }
    }

    public static void putStringIntoDicts(String str){
        char[] chars = str.toCharArray();
        Node node=dicts;
        for (int i = 0; i < str.length(); i++) {
            char ch=chars[i];
            Node nextNode = node.get(ch);
            if (nextNode!=null){
                if (nextNode==LAST_NODE){
                    if (i!=str.length()-1){
                        //不是最后一个字符
                        Node node1 = new Node(true);
                        node.put(ch,node1);
                        nextNode=node1;
                    }
                }
                node=nextNode;
            }else {
                Node newNode;
                if (i==str.length()-1){
                    newNode=LAST_NODE;
                }else {
                    newNode=new Node();
                }
                node.put(ch,newNode);
                node=newNode;
            }
        }
    }


}
